package com.gx.smart.base;

import android.app.Application;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.blankj.utilcode.util.ToastUtils;
import com.casic.gx.grpc.common.ComResp;
import com.gx.smart.lib.http.api.config.ApiConfig;
import com.orhanobut.logger.Logger;


/**
 * @author xiaosy
 * @create 2020/4/1
 * @Describe 公共的viewModel
 **/
public class BaseViewModel extends AndroidViewModel {
    //请求错误
    public MutableLiveData<Boolean> error = new MutableLiveData<>(false);

    public BaseViewModel(@NonNull Application application) {
        super(application);
    }


    /**
     * 统一处理服务器异常
     */
    public void handleServiceException(ComResp resp) {
        Logger.d("msg:" + resp.getMsg());
        Logger.d("err:" + resp.getErr());
        Logger.d("tip:" + resp.getTip());
        String tip = resp.getTip();
        if (TextUtils.isEmpty(tip)) {
            ToastUtils.showLong(ApiConfig.SERVER_ERROR_MESSAGE);
        } else {
            ToastUtils.showLong(tip);
        }
    }

}
